package com.chenyun.system.util;

import com.chenyun.system.model.SysDictData;
import com.chenyun.common.constants.SysConstants;
import com.chenyun.common.context.SpringContext;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;

import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * DictUtils：
 * Description:
 *
 * @author: Lonni
 * @date: 2022/6/17 0017 15:22
 */
public class DictUtils {


    /**
     * 设置字典数据缓存
     *
     * @param key dictType
     * @param dictDatas
     */
    public static void setDictCache(String key, List<SysDictData> dictDatas) {
        RedisTemplate redisTemplate = SpringContext.getBean(RedisTemplate.class);
        String keyString = SysConstants.SYS_DICT_KEY + key;
        redisTemplate.opsForValue().set(keyString, dictDatas);

    }

    /**
     * 获取缓存数据
     * @param key dictType
     * @return
     */
    public static List<SysDictData> getDictCache(String key) {
        RedisTemplate redisTemplate = SpringContext.getBean(RedisTemplate.class);
        String keyString = SysConstants.SYS_DICT_KEY + key;
        Object cacheObj = redisTemplate.opsForValue().get(keyString);
        if (Objects.nonNull(cacheObj)) {
            List<SysDictData> dictDatas = (List<SysDictData>) cacheObj;
            return dictDatas;
        }
        return null;
    }

    public static void removeDictCache(String key) {
        RedisTemplate redisTemplate = SpringContext.getBean(RedisTemplate.class);
        String keyString = SysConstants.SYS_DICT_KEY + key;
        redisTemplate.delete(keyString);

    }

    /**
     * 清楚所有缓存
     */
    public static void clearDictCache() {
        RedisTemplate redisTemplate = SpringContext.getBean(RedisTemplate.class);
        String keyString = SysConstants.SYS_DICT_KEY + "*";
        Set keys = redisTemplate.keys(keyString);
        redisTemplate.delete(keys);
//
    }


}
